package com.lance.quartz.common.config;

import java.io.IOException;
import java.util.Properties;

import com.lance.quartz.service.TaskServiceImpl;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置任务调度中心
 * [QRTZ_JOB_DETAILS], [QRTZ_TRIGGERS] and [QRTZ_CRON_TRIGGERS]
 *
 * @author lance
 */
@Configuration
@ConfigurationProperties(prefix = "org.quartz.dataSource.quartzDataSource")
public class QuartzConfig {

    private String quartzDbDriver;
    private String quartzDbUrl;
    private String quartzDbUser;
    private String quartzDbPasswd;

    public String getQuartzDbDriver() {
        return quartzDbDriver;
    }

    public void setQuartzDbDriver(String quartzDbDriver) {
        this.quartzDbDriver = quartzDbDriver;
    }

    public String getQuartzDbUrl() {
        return quartzDbUrl;
    }

    public void setQuartzDbUrl(String quartzDbUrl) {
        this.quartzDbUrl = quartzDbUrl;
    }

    public String getQuartzDbUser() {
        return quartzDbUser;
    }

    public void setQuartzDbUser(String quartzDbUser) {
        this.quartzDbUser = quartzDbUser;
    }

    public String getQuartzDbPasswd() {
        return quartzDbPasswd;
    }

    public void setQuartzDbPasswd(String quartzDbPasswd) {
        this.quartzDbPasswd = quartzDbPasswd;
    }

    @Autowired
    private MyJobFactory myJobFactory;

    @Bean
    public Scheduler scheduler() throws IOException, SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory(quartzProperties());
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.setJobFactory(myJobFactory);
        scheduler.start();
        return scheduler;
    }


    /**
     * 设置quartz属性
     *
     * @throws IOException 2016年10月8日下午2:39:05
     */
    public Properties quartzProperties() throws IOException {
        Properties prop = new Properties();
        prop.put("quartz.scheduler.instanceName", "ServerScheduler");
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        prop.put("org.quartz.scheduler.instanceId", "NON_CLUSTERED");
        prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
        prop.put("org.quartz.jobStore.tablePrefix", "qrtz_");
        prop.put("org.quartz.jobStore.isClustered", "true");
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "5");

/*        prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.mysql.jdbc.Driver");
        prop.put("org.quartz.dataSource.quartzDataSource.URL", "jdbc:mysql://localhost:3306/mysql");
        prop.put("org.quartz.dataSource.quartzDataSource.user", "root");
        prop.put("org.quartz.dataSource.quartzDataSource.password", "123456");  */
        prop.put("org.quartz.dataSource.quartzDataSource.driver", this.quartzDbDriver);
        prop.put("org.quartz.dataSource.quartzDataSource.URL", this.quartzDbUrl);
        prop.put("org.quartz.dataSource.quartzDataSource.user", this.quartzDbUser);
        prop.put("org.quartz.dataSource.quartzDataSource.password", this.quartzDbPasswd);
        prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "10");
        return prop;
    }
}